package top.infopub.mgr.hr.web;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.ErrorCodeEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.hr.service.PostGroupService;
import top.infopub.mgr.util.LogUtil;
import top.infopub.model.hr.PostGroup;
import top.infopub.model.hr.PostInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.tdp.util.GrpParaUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/postGroup")
public class PostGroupController {
    @Autowired
    PostGroupService postGroupService;

    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        return "hr/lay-postInfo-list";
    }

    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<PostGroup> queryDataByCondition(PostGroup postGroup, ParamPage paramPage) {
        Page<PostGroup> page = new Page<PostGroup>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = postGroupService.selectPageByDomainWhere(page, postGroup);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    @RequestMapping("/save")
    public @ResponseBody RspVo<PostGroup> save(PostGroup postGroup){
        if (StringUtils.isEmpty(postGroup.getName())){
            return ErrorCodeEnum.ERROR_COMMON_PARAM.getRspVo("缺少参数");
        }

        postGroupService.save(postGroup);

        if(StringUtils.isNotEmpty(postGroup.getId())){
            LogUtil.bizLog(LogUtil.LogModelType.OTHER, LogUtil.AcctionType.ADD, "新增岗位组 - 部门名称:" + postGroup.getName().trim(), null);
        }else{
            LogUtil.bizLog(LogUtil.LogModelType.OTHER, LogUtil.AcctionType.UPDATE, "修改岗位组 - 部门Id:" + postGroup.getId(), null);
        }
        return RspVo.getSuccessResponse(postGroup);
    }

    @RequestMapping("/updateStatus")
    public @ResponseBody RspVo<String> updateStatus(PostGroup postGroup) {
        PostGroup group = postGroupService.selectByPrimaryKey(postGroup.getId());
        Integer oldStatus = group.getState();
        Integer newStatus = 0;
        if (oldStatus == 0) {
            newStatus = 1;
        }
        else {
            newStatus = 0;
        }
        group.setState(newStatus);
        group.setUptTime(new Date());
        group.setUptBy(SessionUserUtils.getSessionUserId());
        postGroupService.updateByPrimaryKey(group);
        // 操作日志
        LogUtil.bizLog(
                LogUtil.LogModelType.OTHER,
                LogUtil.AcctionType.UPDATE,
                "修改状态  岗位组:" + group.getName() + " 原状态:"
                        + GrpParaUtil.getName("A00010020", String.valueOf(oldStatus)) + " 被修改为:"
                        + GrpParaUtil.getName("A00010020", String.valueOf(newStatus)), "");
        return RspVo.getSuccessResponse();
    }

    @RequestMapping("/savePost")
    public @ResponseBody RspVo savePost(String postName ,String groupId,String type,String roleType){
        if (StringUtils.isEmpty(postName)||StringUtils.isEmpty(groupId)){
            return ErrorCodeEnum.ERROR_COMMON_PARAM.getRspVo("缺少参数");
        }
        postGroupService.savePost(postName,groupId,type,roleType);
        return RspVo.getSuccessResponse();
    }

    @RequestMapping("/deleteInfo")
    public @ResponseBody RspVo deleteInfo(String id ){
        if (StringUtils.isEmpty(id)){
            return ErrorCodeEnum.ERROR_COMMON_PARAM.getRspVo("缺少参数");
        }
        postGroupService.deleteGroupInfoById(id);
        return RspVo.getSuccessResponse();
    }
}
